package com.bluemsun.naturallanguageprocessing.service.impl;

import com.bluemsun.naturallanguageprocessing.dao.GoodMapper;
import com.bluemsun.naturallanguageprocessing.dao.QuestionMapper;
import com.bluemsun.naturallanguageprocessing.entity.Question;
import com.bluemsun.naturallanguageprocessing.service.ITimeService;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ：hj
 * @date ：Created in 2021/5/6 22:13
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class TimeServiceImpl implements ITimeService {

    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    GoodMapper goodMapper;

    private static Logger logger = LoggerFactory.getLogger(TimeServiceImpl.class);

    @Override
    //晚上2:30执行定时任务
//    @Scheduled(cron = "30 30 2 1/1 * ?")
    public void setHot() {
        logger.info("定时器生效");
        List<Question> list = questionMapper.selectQuestionAndLike();
        String baseSolrUrl ="http://101.200.193.119:8085/solr/core_demo";
        HttpSolrClient client= new HttpSolrClient.Builder(baseSolrUrl).build();
        for(Question l:list){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long startDateTime = 0;
            long endDateTime = 0;
            Date date =new Date();
            try {
                startDateTime = dateFormat.parse(dateFormat.format(l.getCreateTime())).getTime();
                endDateTime = dateFormat.parse(dateFormat.format(date)).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error("定时任务出错"+e);
            }
            double  day= (double) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
            Double hot = (l.getVisitNumber()*0.02+l.getLike()*0.03-day*0.1);
            l.setHot(hot);
            l.setQid(l.getId().toString());
            try {
                UpdateResponse response=client.addBean(l);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (SolrServerException e) {
                e.printStackTrace();
            }

        }
          questionMapper.updateAllHot(list);
        try {
            client.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            logger.error("error solr",e);
            e.printStackTrace();
        }
        try {
            client.close();
        } catch (IOException e) {
            logger.error("error solr",e);
            e.printStackTrace();
        }
    }
}
